package com.goodlearning.gloriamobile.net;

import java.io.Reader;
import java.util.Iterator;

import com.goodlearning.gloriamobile.commons.Utils;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonWrapper extends  JSONParser{
	private String textJson;
	private Reader streamJson;
	private JSONArray jsonArray;

	public JsonWrapper(Reader streamJson) {
		this.streamJson = streamJson;
		try {
			jsonArray = (JSONArray) parse(streamJson);
		} catch (Exception e) {
			Utils.ePrintStackTrace(e);
		}
		
		showLog();
	} 

	public JsonWrapper(String textJson) {
		this.textJson = textJson;
		try {
			jsonArray = (JSONArray) parse(textJson);
		} catch (Exception e) {
			Utils.ePrintStackTrace(e);
		}
		showLog();
	} 
	
	public JSONArray getRootArray() {
		return jsonArray;
	}
	
	private void showLog() {
		if(!Utils.MODE_ON_JSONLIST){
			return;
		}
		
		Utils.log("---------------- json --------------------------");
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObj = (JSONObject)jsonArray.get(i);
			Iterator it = jsonObj.keySet().iterator();
			Utils.log("-----------  ITEM_"+i+"-----------------");
			while(it.hasNext()){
				Object key = it.next();
				Utils.log( key+ " : "+jsonObj.get(key) );
			}
			
		}

	}
	
	



}
